package com.xl.bigdata.tools.zookeeper;

import org.apache.curator.framework.recipes.locks.InterProcessMutex;

/**
 * @program: IAEngine
 * @description:
 * @author: XL.Gao
 * @create: 2021-05-24 11:12
 **/
public class CuratorThread implements Runnable {

    private Integer threadFlag;

    private InterProcessMutex lock;

    public CuratorThread(Integer threadFlag, InterProcessMutex lock){
        this.threadFlag = threadFlag;
        this.lock = lock;
    }

    @Override
    public void run() {

        try {
            long start = System.currentTimeMillis();

            lock.acquire();

            System.out.println("第"+threadFlag+"线程获取到了锁 -- 获取锁的时间" + (System.currentTimeMillis() - start));

            //等到1秒后释放锁
            Thread.sleep(10000);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                lock.release();

                System.out.println("第"+threadFlag+"线程释放锁");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
